package cn.iocoder.yudao.module.member.assets.mq;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.module.api.brokerage.record.message.CommissionCancelMessage;
import cn.iocoder.yudao.module.api.brokerage.record.message.CommissionUnfreezeMessage;
import cn.iocoder.yudao.module.api.member.user.message.MemberUserCreateMessage;
import cn.iocoder.yudao.module.api.promotion.gift.message.OnlineProductGiftTakeMessage;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.revenue.message.RevenueCancelMessage;
import cn.iocoder.yudao.module.api.shop.revenue.message.RevenueUnfreezeMessage;
import cn.iocoder.yudao.module.member.assets.service.AppMemberDigitalAssetsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 数字资产 事件消费者
 *
 * @author 山野羡民
 */
@Component
@Slf4j
public class MemberDigitalAssetsConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(MemberUserCreateMessage message) {
        log.info("[member/assets][onMessage][用户创建事件({})]", message);
        SpringUtil.getBean(AppMemberDigitalAssetsService.class).getOrCreate(message.getUserId());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(CommissionUnfreezeMessage message) {
        log.info("[member/assets][onMessage][佣金结算事件({})]", message);
        SpringUtil.getBean(AppMemberDigitalAssetsService.class).changeCommission(message.getUserId(), 1d * message.getSettlementMoney());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(CommissionCancelMessage message) {
        log.info("[member/assets][onMessage][佣金取消事件({})]", message);
        SpringUtil.getBean(AppMemberDigitalAssetsService.class).changeCommission(message.getUserId(), -1d * message.getSettlementMoney());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(RevenueUnfreezeMessage message) {
        log.info("[member/assets][onMessage][商家营收结算事件({})]", message);
        Long userId = SpringUtil.getBean(ShopInfoApi.class).getOwnerMemberUserId(message.getShopId());
        SpringUtil.getBean(AppMemberDigitalAssetsService.class).changeRevenue(userId, 1d * message.getSettlementMoney());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(RevenueCancelMessage message) {
        log.info("[member/assets][onMessage][商家营收取消事件({})]", message);
        Long userId = SpringUtil.getBean(ShopInfoApi.class).getOwnerMemberUserId(message.getShopId());
        SpringUtil.getBean(AppMemberDigitalAssetsService.class).changeRevenue(userId, -1d * message.getSettlementMoney());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(OnlineProductGiftTakeMessage message) {
        log.info("[member/assets][onMessage][线上产品类型的赠品领取事件({})]", message);
        Long userId = SpringUtil.getBean(ShopInfoApi.class).getOwnerMemberUserId(message.getShopId());
        SpringUtil.getBean(AppMemberDigitalAssetsService.class).changeRevenue(userId, 1d * message.getSettlementMoney());
    }

}
